package com.ct.application.shipping.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ct.application.shipping.model.po.ChuanYuanShenHeSearchPO;
import com.ct.application.shipping.service.ChuanYuanKuService;
import com.ct.repository.commons.BaseServiceImpl;
import com.ct.shipping.mapper.ChuanYuanKuShenHeMapper;
import com.ct.shipping.orm.ChuanYuanKuDO;
import com.ct.shipping.orm.ChuanYuanKuShenHeDO;
import com.ct.utils.DefaultUtil;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 船员库审核 服务实现类
 * </p>
 *
 * @author carlo
 */
@Service
public class ChuanYuanKuShenHeServiceImpl extends BaseServiceImpl<ChuanYuanKuShenHeMapper, ChuanYuanKuShenHeDO>
        implements ChuanYuanKuShenHeService {

    @Resource
    private ChuanYuanKuService chuanYuanKuService;

    /**
     * 查询职位分页列表
     */
    public Page<ChuanYuanKuShenHeDO> getList(ChuanYuanShenHeSearchPO searchPO) {
        LambdaQueryWrapper<ChuanYuanKuShenHeDO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(StringUtils.isNotEmpty(searchPO.getXingMing()), ChuanYuanKuShenHeDO::getXingMing, searchPO.getXingMing());
        lambdaQueryWrapper.like(StringUtils.isNotEmpty(searchPO.getShenFenHao()), ChuanYuanKuShenHeDO::getShenFenHao, searchPO.getShenFenHao());
        lambdaQueryWrapper.eq(StringUtils.isNotEmpty(searchPO.getZhuangTai()), ChuanYuanKuShenHeDO::getZhuangTai, searchPO.getZhuangTai());
        lambdaQueryWrapper.orderByDesc(ChuanYuanKuShenHeDO::getCreateTime);

        Page<ChuanYuanKuShenHeDO> page = new Page<>(searchPO.getPageNo(), searchPO.getPageSize());
        Page<ChuanYuanKuShenHeDO> page1 = this.page(page, lambdaQueryWrapper);
        return page1;
    }


    /**
     * 审核通过
     */
    @Transactional(rollbackFor = Exception.class)
    public boolean tongGuo(Long id) {
        if (null == id) {
            throw new RuntimeException("id不能为空");
        }
        ChuanYuanKuShenHeDO chuanYuanKuShenHeDO = this.getById(id);
        if (chuanYuanKuShenHeDO == null) {
            throw new RuntimeException("操作失败，查无此数据");
        }

        String xingMing = chuanYuanKuShenHeDO.getXingMing();
        String shenFenHao = chuanYuanKuShenHeDO.getShenFenHao();
        String shouJiHao = chuanYuanKuShenHeDO.getShouJiHao();
        String zhiWei = chuanYuanKuShenHeDO.getZhiWei();
        String zhengShuDengJi = chuanYuanKuShenHeDO.getZhengShuDengJi();
        Long chuanYuanId = chuanYuanKuShenHeDO.getChuanYuanId();
        if (chuanYuanId != null &&  chuanYuanId > 0) {
            chuanYuanKuService.lambdaUpdate()
                    .set(StringUtils.isNotEmpty(xingMing),ChuanYuanKuDO::getXingMing,xingMing)
                    .set(StringUtils.isNotEmpty(shenFenHao),ChuanYuanKuDO::getShenFenHao,shenFenHao)
                    .set(StringUtils.isNotEmpty(shouJiHao),ChuanYuanKuDO::getShouJiHao,shouJiHao)
                    .set(StringUtils.isNotEmpty(zhiWei),ChuanYuanKuDO::getZhiWei,zhiWei)
                    .set(StringUtils.isNotEmpty(zhengShuDengJi),ChuanYuanKuDO::getZhengShuDengJi,zhengShuDengJi)
                    .eq(ChuanYuanKuDO::getId,chuanYuanId)
                    .update();
        } else {
            ChuanYuanKuDO chuanYuanKuDO = new ChuanYuanKuDO();
            chuanYuanKuDO.setXingMing(DefaultUtil.stringDefault(xingMing));
            chuanYuanKuDO.setShenFenHao(DefaultUtil.stringDefault(shenFenHao));
            chuanYuanKuDO.setShouJiHao(DefaultUtil.stringDefault(shouJiHao));
            chuanYuanKuDO.setZhiWei(DefaultUtil.stringDefault(zhiWei));
            chuanYuanKuDO.setZhengShuDengJi(DefaultUtil.stringDefault(zhengShuDengJi));
            chuanYuanKuDO.insert();
            chuanYuanId = chuanYuanKuDO.getId();
        }

        ChuanYuanKuShenHeDO update = new ChuanYuanKuShenHeDO();
        update.setId(id);
        update.setZhuangTai("已审核");
        update.setChuanYuanId(chuanYuanId);
        update.updateById();
        return true;
    }






    /* =======================↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓H5页面接口↓↓↓↓↓↓↓↓↓↓==================================== */
    /* =======================↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓H5页面接口↓↓↓↓↓↓↓↓↓↓==================================== */


    /**
     * 根据openid询审核船员资料
     */
    public ChuanYuanKuShenHeDO getByOpenid(String openid) {
        if (null == openid) {
            throw new RuntimeException("查询失败，缺少openid");
        }
        return this.lambdaQuery().eq(ChuanYuanKuShenHeDO::getOpenid,openid).one();
    }


    /**
     * 保存
     */
    @Transactional(rollbackFor = Exception.class)
    public boolean saveChuanYuanKuShenHe(ChuanYuanKuShenHeDO savePO) {
        if (StringUtils.isEmpty(savePO.getShouJiHao())) {
            throw new RuntimeException("请填写手机号");
        }
        if (StringUtils.isEmpty(savePO.getOpenid())) {
            throw new RuntimeException("保存失败，缺少openid");
        }

        savePO.setId(null); //新增不需要id
        savePO.setShiFouCunZai("否");
        savePO.setZhuangTai("未审核");
        if (StringUtils.isEmpty(savePO.getShenFenHao())) {
            //根据身份号(如果有填写)判断在船员库中是否存在，存在用一个字段标识为已存在
            ChuanYuanKuDO ChuanYuan = chuanYuanKuService.lambdaQuery()
                    .eq(ChuanYuanKuDO::getShenFenHao, savePO.getShenFenHao())
                    .last(" limit 1 ")
                    .one();
            if (ChuanYuan != null) {
                savePO.setShiFouCunZai("是");
                savePO.setChuanYuanId(ChuanYuan.getId());
            }
        }
        return savePO.insert();
    }


    /**
     * 修改
     */
    @Transactional(rollbackFor = Exception.class)
    public boolean updateChuanYuanKuShenHe(ChuanYuanKuShenHeDO savePO) {
        if (savePO.getId() == null) {
            throw new RuntimeException("保存失败，缺少主键id");
        }
        if (StringUtils.isEmpty(savePO.getShouJiHao())) {
            throw new RuntimeException("请填写手机号");
        }
        if (StringUtils.isEmpty(savePO.getOpenid())) {
            throw new RuntimeException("保存失败，缺少openid");
        }

        savePO.setShiFouCunZai("否");
        if (StringUtils.isEmpty(savePO.getShenFenHao())) {
            //根据身份号(如果有填写)判断在船员库中是否存在，存在用一个字段标识为已存在
            ChuanYuanKuDO ChuanYuan = chuanYuanKuService.lambdaQuery()
                    .eq(ChuanYuanKuDO::getShenFenHao, savePO.getShenFenHao())
                    .last(" limit 1 ")
                    .one();
            if (ChuanYuan != null) {
                savePO.setShiFouCunZai("是");
                savePO.setChuanYuanId(ChuanYuan.getId());
            }
        }
        return savePO.updateById();
    }



}
